package com.boarsoft.boar.batch.listener;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.batch.service.BatchExecCtrlService;
import com.boarsoft.boar.batch.service.BatchReportService;
import com.boarsoft.flow.core.SimpleFlow;
import com.boarsoft.flow.core.bean.BaseNode;
import com.boarsoft.flow.event.SimpleFlowListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * onStop事件产生时，判断状态并上报给批量调度端
 *
 * @author tao.Wang57
 * @date 2024-01-12
 */
public class BatchStsListener implements SimpleFlowListener {

    private static Logger log = LoggerFactory.getLogger(BatchStsListener.class);

    @Autowired
    private BatchReportService batchReportService;

    @Autowired
    private BatchExecCtrlService batchExecCtrlService;

    /**
     * onStop事件发生时需要上报批量调度进行状态确认
     *
     * @param flow
     */
    @Override
    public void onStop(SimpleFlow flow) {
        String batchId = MDC.get("intdNo");
        log.info(" start report BatchStsListener.onStop.{}  event.", batchId);
        short sts = batchExecCtrlService.getStatus(batchId);
        log.info(" 订单号：{}, 对应的状态为：{}", batchId, sts);
        ReplyInfo<Object> result = batchReportService.cmdReport(batchId, sts);
        log.info(" end report BatchStsListener.onStop.{}  event. 上报结果：", batchId, result.isSuccess() ? "SUCCESS" : "FAILD");
    }

    @Override
    public void onError(SimpleFlow flow) {

    }

    @Override
    public void onSuspend(SimpleFlow flow) {

    }

    @Override
    public void onTimeout(SimpleFlow flow) {

    }

    @Override
    public void onBegin(SimpleFlow flow) {

    }

    @Override
    public void onEnd(SimpleFlow flow) {

    }

    @Override
    public void onBeforeStep(SimpleFlow flow, Object data, BaseNode next) {

    }

    @Override
    public void onAfterStep(SimpleFlow flow, Object data, Throwable flowEx, String next, Throwable stepEx, long time) {

    }

    @Override
    public void onBeforeHandle(SimpleFlow flow, Object data, Throwable flowEx, Object dd) {

    }

    @Override
    public void onAfterHandle(SimpleFlow flow, Object data, Throwable flowEx, Object checkInData, Object dataCheckOut, Throwable handleEx) {

    }

    @Override
    public void onFailHandle(SimpleFlow flow, Object data, Throwable flowEx, Object checkInData, Throwable handleEx) {

    }

    @Override
    public void onFailStep(SimpleFlow flow, Object data, Throwable flowEx, String step, Throwable stepEx) {

    }
}
